﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;

namespace dhsystem.orderForm
{
         
    public partial class FrmSupplierAdd : Form
    {
        supplierTypeBLL supplierType = new supplierTypeBLL();
        supplierListModel supplierRow = new supplierListModel();
        supplierListBLL supplier = new supplierListBLL();
        FrmSupplierList supplierList = new FrmSupplierList("");
        BindingSource BindingSourceSupplierAdd;
        string state;
        //绑定数据源
        public FrmSupplierAdd(BindingSource bindingSourceSupplierList, string _state)
        {
            InitializeComponent();
            BindingSourceSupplierAdd = bindingSourceSupplierList;
            state = _state;
        }
        //打开窗口时绑定数据
        private void FrmSupplierAdd_Load(object sender, EventArgs e)
        {
            textsName.Focus();
          
            int  s;
            Random r = new Random();
           s=r.Next (1000000000);
            textsNum.Text = DateTime.Now.ToShortDateString() + "-"+s;
            bindingSourcesupplierType.DataSource = supplierType.getsupplierTypeAll();
            comboBoxsType.DataSource = bindingSourcesupplierType;
            comboBoxsType.DisplayMember = "sType";
            comboBoxsType.ValueMember = "sType";
            if (state == "编辑")
            {
                textsNum.DataBindings.Add("text", BindingSourceSupplierAdd, "sNum");
                textsName.DataBindings.Add("text", BindingSourceSupplierAdd, "sName");
                textlinkman.DataBindings.Add("text", BindingSourceSupplierAdd, "linkman");
                textphone.DataBindings.Add("text", BindingSourceSupplierAdd, "phone");
                textmail.DataBindings.Add("text", BindingSourceSupplierAdd, "mail");
                textqq.DataBindings.Add("text", BindingSourceSupplierAdd, "qq");
                textfax.DataBindings.Add("text", BindingSourceSupplierAdd, "fax");
                textaddres.DataBindings.Add("text", BindingSourceSupplierAdd, "addres");
                textpayType.DataBindings.Add("text", BindingSourceSupplierAdd, "payType");
                textbillType.DataBindings.Add("text", BindingSourceSupplierAdd, "billType");
                comboBoxsType.DataBindings.Add("text", BindingSourceSupplierAdd, "sType");
                textCreateUser.DataBindings.Add("text", BindingSourceSupplierAdd, "createUser");
                textUpdateUser.DataBindings.Add("text", BindingSourceSupplierAdd, "updateUser");
                textmark.DataBindings.Add("text", BindingSourceSupplierAdd, "mark");
            }

        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增供应商资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        supplierRow.sNum = textsNum.Text;
                        supplierRow.sName = textsName.Text;
                        supplierRow.linkman = textlinkman.Text;
                        supplierRow.phone = textphone.Text;
                        supplierRow.mail = textmail.Text;
                        supplierRow.qq = textqq.Text;
                        supplierRow.fax = textfax.Text;
                        supplierRow.addres = textaddres.Text;
                        supplierRow.payType = textpayType.Text;
                        supplierRow.billType = textbillType.Text;
                        supplierRow.sType = comboBoxsType.Text;
                        supplierRow.createDate = DateTime.Now;
                        supplierRow.createUser = textCreateUser.Text;
                        supplierRow.updateDate = DateTime.Now;
                        supplierRow.updateUser = textUpdateUser.Text;
                        supplierRow.mark = textmark.Text;
                        supplier.supplierListInsert(supplierRow);
                        bindingSourceSupplierList.DataSource = supplier.getsupplierListAll();
                        MessageBox.Show("新增成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改供应商资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        supplierRow.sNum = textsNum.Text;
                        supplierRow.sName = textsName.Text;
                        supplierRow.linkman = textlinkman.Text;
                        supplierRow.phone = textphone.Text;
                        supplierRow.mail = textmail.Text;
                        supplierRow.qq = textqq.Text;
                        supplierRow.fax = textfax.Text;
                        supplierRow.addres = textaddres.Text;
                        supplierRow.payType = textpayType.Text;
                        supplierRow.billType = textbillType.Text;
                        supplierRow.sType = comboBoxsType.Text; 
                        supplierRow.createUser = textCreateUser.Text;
                        supplierRow.updateDate = DateTime.Now;
                        supplierRow.createDate = DateTime.Now;
                        supplierRow.updateUser = textUpdateUser.Text;
                        supplierRow.mark = textmark.Text;
                        supplier.supplierListUpdate (supplierRow);
                        bindingSourceSupplierList.DataSource = supplier.getsupplierListAll();
                        MessageBox.Show("修改成功");

                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textsNum_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
